<?php
/*
 *
 *      urle.plugin.php - v1.0.0 @07.07.2009 22:29:40 CEST
 *
 *      StudioGallery Copyright (C) 2009 Rafal Kusior (spuavick) <spuavick@interia.pl>
 *
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *      -----------------------------------------------------------------------------
 *
 *      Niniejszy program jest wolnym oprogramowaniem - możesz go rozpowszechniać
 *      dalej i/lub modyfikować na warunkach Powszechnej Licencji Publicznej GNU
 *      wydanej przez Fundację Wolnego Oprogramowania, według wersji 2 tej Licencji
 *      lub dowolnej z późniejszych wersji.
 *
 *      Niniejszy program rozpowszechniany jest z nadzieją, iż będzie on użyteczny -
 *      jednak BEZ ŻADNEJ GWARANCJI, nawet domyślnej gwarancji PRZYDATNOŚCI HANDLOWEJ
 *      albo PRZYDATNOŚCI DO OKREŚLONYCH ZASTOSOWAŃ. Bliższe informacje na ten temat
 *      można uzyskać z Powszechnej Licencji Publicznej GNU.
 *
 *      Kopia Powszechnej Licencji Publicznej GNU powinna zostać ci dostarczona
 *      razem z tym programem. Jeżeli nie została dostarczona, odwiedź
 *      <http://www.gnu.org/licenses/>
 */

$plugin_id = basename(__FILE__);

$data['name'] = "Przyjazne URLe";
$data['desc'] = "Wtyczka podmienia linki na przyjazne";
$data['author'] = "spuavick";
$data['url'] = "http://www.spuavick.pl/";
$data['version'] = '1.0.0';
$data['options'] = false; /**
			    *	serialize(array) jeśli ustawienia domyślne;
			    *	true jeśli istnieją opcje dla pluginu, ale ustawione będą przez użytkownika;
			    *	false jeśli nie ma opcji
			    */


if (plugin_status($plugin_id,$data) == '1' ) {

	add_hook('photos','sg_urle_photos');
	add_hook('cat_photo','sg_urle_cat_photo');
	add_hook('newslist','sg_urle_newslist');
	add_hook('newsitem','sg_urle_newsitem');
	add_hook('navigation','sg_urle_navigation');
	add_hook('lastnews','sg_urle_lastnews');
	add_hook('pagination','sg_urle_pagination');
	add_hook('menu','sg_urle_menu');
	add_hook('cat_gallery','sg_urle_cat_gallery');

}

if ($_GET['activate'] && $_GET['activate'] == $plugin_id) {

    add_hook('plugin_info','sg_urle_install');

}

function sg_urle_install() {

	global $conf;
	if ( sg_urle_pre_install() ) {
    	return '<div class="ok">Wtyczka zainstalowana</div>';
	} else {

		preg_match("#http\:\/\/(.*?)\/(.*?)?$#",$conf['conf_url'],$baseUrl);
		if (!$baseUrl[2] || $baseUrl[2] == '/')
			$base = '/';
		else
			$base = preg_replace("#^\/?(.*?)\/?$#","/$1/",$baseUrl[2]);

    	return '<div class="error">UWAGA!<div style="font-size:14px;">Wtyczka nie była w stanie przeprowadzić zmian w pliku <strong>.htaccess</strong>.<br/>Konieczne jest ręczne dodanie poniższego wpisu:<div><pre style="text-align:left; font-weight:normal;font-size:12px; background:#fff; color:#000; border:1px solid #ddd; padding:20px;margin:10px">Options FollowSymLinks
RewriteEngine On
RewriteBase '.$base.'
RewriteRule ^index.html$ index.php [L]
RewriteRule ^news,([0-9a-z\-]+),([0-9]+).html$ news.php?id=$2 [L]
RewriteRule ^news,([0-9]+).html$ news.php?s=$1 [L]
RewriteRule ^news$ news.php [L]
RewriteRule ^photo,([0-9A-Za-z]{32}).html$ photo.php?idp=$1 [L]
Rewriterule ^gallery,([0-9]+),([0-9]+).html$ gallery.php?cat=$1&s=$2 [L]
RewriteRule ^gallery,([0-9]+).html$ gallery.php?cat=$1 [L]
RewriteRule ^gallery$ gallery.php [L]
RewriteRule ^([0-9a-z\-]+).html$ page.php?page=$1 [L]</pre>
    </div></div></div>';
}

}

function sg_urle_pre_install() {

	global $pre,$conf;
	if ( !checkPermission($preU.".htaccess") ) {
    	@chmod($preU.".htaccess",0755);
    	if ( !checkPermission($preU.".htaccess")) {
			return false;
    	}
	}
	$htaccess = $preU.".htaccess";

	preg_match("#http\:\/\/(.*?)\/(.*?)?$#",$conf['conf_url'],$baseUrl);
	if (!$baseUrl[2] || $baseUrl[2] == '/')
		$base = '/';
	else
		$base = preg_replace("#^\/?(.*?)\/?$#","/$1/",$baseUrl[2]);

	$content = 'Options FollowSymLinks
RewriteEngine On
RewriteBase '.$base.'
RewriteRule ^index.html$ index.php [L]
RewriteRule ^news,([0-9a-z\-]+),([0-9]+).html$ news.php?id=$2 [L]
RewriteRule ^news,([0-9]+).html$ news.php?s=$1 [L]
RewriteRule ^news$ news.php [L]
RewriteRule ^photo,([0-9A-Za-z]{32}).html$ photo.php?idp=$1 [L]
Rewriterule ^gallery,([0-9]+),([0-9]+).html$ gallery.php?cat=$1&s=$2 [L]
RewriteRule ^gallery,([0-9]+).html$ gallery.php?cat=$1 [L]
RewriteRule ^gallery$ gallery.php [L]
RewriteRule ^tag-([^-]+).html$ tags.php?tag=$1 [L]
RewriteRule ^([0-9a-z\-]+).html$ page.php?page=$1 [L]';
	$plik = fopen($htaccess, "a+");

	if (flock($plik, LOCK_EX)) {
		fwrite($plik, $content);
		flock($plik, LOCK_UN);
	} else {
		echo _("Problem z plikiem");
		echo " <strong>.htaccess</strong> !";
	}
	fclose($plik);
	@chmod($htaccess,0644);
	return true;
}

function sg_urle_photos($photos) {

	$i=0;
	while($i<count($photos)) {
		$photos[$i]['link'] = preg_replace("#photo\.php\?idp=([0-9A-Za-z]{32})#","photo,$1.html",$photos[$i]['link']);
		$photos[$i]['com_link'] = preg_replace("#photo\.php\?idp=([0-9A-Za-z]{32})#","photo,$1.html",$photos[$i]['com_link']);
		$i++;
	}
	return $photos;
}

function sg_urle_cat_photo($photo) {

	$i=0;
	while($i<count($photo)) {
		$photo[$i]['linkg'] = preg_replace("#gallery\.php\?cat=([0-9]+)#","gallery,$1.html",$photo[$i]['linkg']);
		$photo[$i]['linkt'] = preg_replace("#gallery\.php\?cat=([0-9]+)#","gallery,$1.html",$photo[$i]['linkt']);
		$i++;
	}
	return $photo;
}

function sg_urle_newslist($news) {

	$i=0;
	while($i<count($news)) {
		preg_match("#<a(.*)>(.*?)</a>#",$news[$i]['news_title'],$title[$i]);
		$title[$i][2] = sg_url($title[$i][2]);
		$news[$i]['com_link'] = preg_replace("#news\.php\?id=([0-9]+)#","news,".$title[$i][2].",$1.html",$news[$i]['com_link']);
		$news[$i]['news_text'] = preg_replace("#news\.php\?id=([0-9]+)#","news,".$title[$i][2].",$1.html",$news[$i]['news_text']);
		$news[$i]['news_text'] = preg_replace("#photo\.php\?idp=([0-9a-zA-Z]{32})#","photo,$1.html",$news[$i]['news_text']);
		$news[$i]['news_title'] = preg_replace("#news\.php\?id=([0-9]+)#","news,".$title[$i][2].",$1.html",$news[$i]['news_title']);
		$i++;
	}
	return $news;
}

function sg_urle_convert($matches) {

	$i=0;
	$pre_title = sg_url($matches[4]);
	$matches[1] = preg_replace("#news\.php\?id=([0-9]+)#","news,".$pre_title.",$1.html",$matches[1]);
	$matches[2]='';
	$matches[3] = $matches[3];
	return $matches[1].'" '.$matches[3].'>'.trim($matches[4]).'</a>';
}

function sg_urle_navigation($navi) {

	$navi = preg_replace_callback('#(news\.php\?id=([0-9]+))\"(.*?)>(.*?)</a>#',"sg_urle_convert",$navi);
	$navi = preg_replace("#photo\.php\?idp=([0-9a-zA-Z]{32})#","photo,$1.html",$navi);
	return $navi;
}

function sg_urle_sidebar_tags($sidebar) {

	$sidebar = preg_replace("#tags.php?tag=([a-zA-Z0-9\+\_\-]+)#","tags,$1.html",$sidebar);
	return $sidebar;
}

function sg_urle_lastnews($lastnews) {

	return sg_urle_navigation($lastnews);
}

function sg_urle_pagination($pagin) {

	$pagin = preg_replace("#news\.php\?s=([0-9]+)#","news,$1.html",$pagin);
	$pagin = preg_replace("#gallery\.php\?cat=([0-9]+)\&amp\;s=([0-9]+)#","gallery,$1,$2.html",$pagin);
    return $pagin;
}

function sg_urle_menu($menu) {

	$menu = preg_replace("#gallery\.php\?cat=([0-9]+)#","gallery,$1.html",$menu);
	$menu = preg_replace("#gallery\.php#","gallery",$menu);
	$menu = preg_replace("#news\.php#","news",$menu);
	$menu = preg_replace_callback("#(page\.php\?page=([0-9a-z\-]+))\">(.*?)</a>#","sg_urle_menu_callback",$menu);
	return $menu;
}

function sg_urle_menu_callback ($matches) {

	return sg_url_clear($matches[2]).".html\">".$matches[2].'</a>';
}

function sg_urle_newsitem($news) {

	$news['news_text'] = preg_replace("#photo\.php\?idp=([0-9A-Za-z]{32})#","photo,$1.html",$news['news_text']);
	return $news;
}

function sg_urle_cat_gallery($link) {

	$link = preg_replace("#gallery\.php\?cat=([0-9]+)#","gallery,$1.html",$link);
	$link = preg_replace("#gallery\.php#","gallery",$link);
    return $link;
}
?>
